/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOs;

import entidad.Person;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Stiv
 */
public class DaoPerson {
    
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("GRLUPU");
    public void crear(Person object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
    
     public List leer(String par){
        EntityManager em = emf.createEntityManager();
        List cat=null;
        par = "%"+ par + "%";
        Query q= em.createQuery("SELECT u from Person u "+
                "WHERE u.nombre = :Nombre")
                .setParameter("Nombre", par)
                /*.setParameter("password", par.getPassword())*/;
        try {
            cat = q.getResultList();
        } catch (NonUniqueResultException e) {
            cat = (List) q.getResultList().get(0);
            
        }
        catch(Exception e){
            e.printStackTrace();;            
        }
        finally {
            em.close();
            return cat;
        }
    }
    
}
